package com.hhhn.wk.app;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.support.multidex.MultiDex;
import android.util.Log;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.hhhn.wk.R;
import com.hhhn.wk.utils.CrashHandler;
import com.hhhn.wk.utils.SharePreferencesUser;
import com.hhhn.wk.widget.select_img.imageloader.FrescoImageLoader;
import com.imnjh.imagepicker.PickerConfig;
import com.imnjh.imagepicker.SImagePicker;
import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;
//import com.umeng.message.IUmengRegisterCallback;
//import com.umeng.message.PushAgent;
import com.umeng.socialize.PlatformConfig;


import org.xutils.BuildConfig;
import org.xutils.x;

import java.util.ArrayList;

import static com.umeng.commonsdk.stateless.UMSLEnvelopeBuild.mContext;

/**
 * * * * * * * * * * * *
 * author：WenKe  ｡◕‿◕｡ *
 * Data：2017     ｡◕‿◕｡ *
 * QQ：370820580  ｡◕‿◕｡ *
 * Function：     ｡◕‿◕｡ *
 * 文珂
 * <p>
 * * * * * * * * * * *
 */
public class MyApplication extends Application {
    private static Application instance;
    private static String str_adress = "定位中";
    private static String APKDOWNURL = "https://fir.im/kv6x";
    private static String APPNAMEAZ = "hhhn.apk";//app名字
    private static SharePreferencesUser userShare;//全局用户信息的文件
    ArrayList<Activity> list = new ArrayList<Activity>();
    public static boolean isDebug = true;
//    public PushAgent mPushAgent = null;
    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        /**
         * 初始化common库
         * 参数1:上下文，不能为空
         * 参数2:【友盟+】 AppKey
         * 参数3:【友盟+】 Channel
         * 参数4:设备类型，UMConfigure.DEVICE_TYPE_PHONE为手机、UMConfigure.DEVICE_TYPE_BOX为盒子，默认为手机
         * 参数5:Push推送业务的secret
         */
        UMConfigure.init(this, "5aeffc89b27b0a51b5000100", "Umeng", UMConfigure.DEVICE_TYPE_PHONE, "87f9fb37786493ed89da8088a021eff5");
        initUM();
        //初始化
        initialize();
    }
    public static Context getAppContext() {
        return instance;
    }
    private void initialize() {
        //xUtil
        x.Ext.init(this);
        x.Ext.setDebug(BuildConfig.DEBUG);
        Fresco.initialize(this);
        userShare = new SharePreferencesUser();
        SImagePicker.init(new PickerConfig.Builder().setAppContext(this)
                .setImageLoader(new FrescoImageLoader())
                .setToolbaseColor(getResources().getColor(R.color.colorPrimary)).build());

        //        //设置该CrashHandler为程序的默认处理器
        CrashHandler catchExcep = new CrashHandler(this);
        Thread.setDefaultUncaughtExceptionHandler(catchExcep);
    }
    //友盟
    private void initUM() {
//        /**
//         * 设置组件化的Log开关
//         * 参数: boolean 默认为false，如需查看LOG设置为true
//         */
        UMConfigure.setLogEnabled(true);
//        mPushAgent = PushAgent.getInstance(this);
//        //注册推送服务，每次调用register方法都会回调该接口
//        mPushAgent.register(new IUmengRegisterCallback() {
//            @Override
//            public void onSuccess(String deviceToken) {
//                //注册成功会返回device token
//                Log.e("友盟", "onSuccess: " + deviceToken);
//                Log.e("友盟", "onSuccess: " + mPushAgent.getRegistrationId());
//            }
//            @Override
//            public void onFailure(String s, String s1) {
//            }
//        });
       // PlatformConfig.setWeixin("wx0242bc511b52ce4f", "2d81eab6249a75f9bc9cc463f05f9ab5");
        PlatformConfig.setWeixin("wx0242bc511b52ce4f", "84461a9513b6785dca5b34131b3dd0fe");
        MobclickAgent.setScenarioType(this, MobclickAgent.EScenarioType.E_UM_NORMAL);
    }
    //用于防止在混淆编译时出现编译的包过多，出现程序运行问题
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
    public SharePreferencesUser getWKSharedHelper() {
        Log.e("用户全局app", "getWKSharedHelper: " + userShare);
        if (userShare == null) {
            userShare = new SharePreferencesUser();//初始化用户全局
        }
        return userShare;
    }
    public static String getAPKDOWNURL() {
        return APKDOWNURL;
    }
    public static void setAPKDOWNURL(String APKDOWNURL) {
        MyApplication.APKDOWNURL = APKDOWNURL;
    }
    public static String getAPPNAMEAZ() {
        return APPNAMEAZ;
    }
    public static void setAPPNAMEAZ(String APPNAMEAZ) {
        MyApplication.APPNAMEAZ = APPNAMEAZ;
    }
    //Activity关闭时，删除Activity列表中的Activity对象
    public void removeActivity(Activity a) {
        list.remove(a);
    }
    //向Activity列表中添加Activity对象
    public void addActivity(Activity a) {
        list.add(a);
    }
    //关闭Activity列表中的所有Activity
    public void finishActivity() {
        for (Activity activity : list) {
            if (null != activity) {
                activity.finish();
            }
        }
        // 杀死该应用进程
        android.os.Process.killProcess(android.os.Process.myPid());
    }
    public ArrayList<Activity> getList() {
        return list;
    }
    public void setList(ArrayList<Activity> list) {
        this.list = list;
    }
    public boolean getIsDebug() {
        return isDebug;
    }
    //填充定位
    public static String getStr_adress() {
        return str_adress;
    }

    //获取定位
    public static void setStr_adress(String str_adress) {
        MyApplication.str_adress = str_adress;
        Log.d("APP获取定位", "setStr_adress: " + str_adress);
    }


}
